var gulp = require('gulp');
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var runSequence = require('run-sequence');
var browserSync = require('browser-sync');
var uglify = require('gulp-uglify');
var eslint = require('gulp-eslint');
var concat = require('gulp-concat');
var reload = browserSync.reload;
var $ = require('gulp-load-plugins')();
var ts = require('gulp-typescript');
var del = require('del');

gulp.task('clean', function () {
  return del('dist')
});

gulp.task('copy:dev', function () {
  const jsNPMDependencies = [
    'angular2/bundles/angular2-polyfills.js',
    'systemjs/dist/system.src.js',
    'rxjs/bundles/Rx.js',
    'angular2/bundles/angular2.dev.js',
    'angular2/bundles/router.dev.js',
    'angular2/bundles/upgrade.dev.js',
    'angular2/bundles/http.dev.js',
  ];

  var mappedPaths = jsNPMDependencies.map(file => {
    return path.resolve('node_modules', file)
  })
  gulp.src(mappedPaths, {base: 'node_modules'})
    .pipe(concat('angular.js'))
    .pipe(gulp.dest('dist/lib'))
  gulp.src(['src/index.html'])
    .pipe(gulp.dest('dist'))
  gulp.src('src/app/*/views/*')
    .pipe(gulp.dest('dist/app/'))
});

gulp.task('copy:prod', function () {
  const jsNPMDependencies = [
    'angular2/bundles/angular2-polyfills.min.js',
    'systemjs/dist/system.js',
    'rxjs/bundles/Rx.min.js',
    'angular2/bundles/angular2.min.js',
    'angular2/bundles/router.min.js',
    'angular2/bundles/upgrade.min.js',
    'angular2/bundles/http.min.js',
  ];
  var mappedPaths = jsNPMDependencies.map(file => {
    return path.resolve('node_modules', file)
  })
  gulp.src(mappedPaths, {base: 'node_modules'})
    .pipe(concat('angular.js'))
    .pipe(gulp.dest('dist/lib'))
});

gulp.task('views', function () {
  gulp.src(['src/index.html'])
    .pipe(gulp.dest('dist'))
  gulp.src('src/app/*/views/*')
    .pipe(gulp.dest('dist/app/'))
});

gulp.task('ts:dev', function () {
  var tsProject = ts.createProject('src/tsconfig.json');
  var tsResult = gulp.src(['src/*.ts','src/**/*.ts'])
    .pipe(sourcemaps.init())
    .pipe(tsProject(tsResult))
  return tsResult.js
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('dist'))
});

gulp.task('ts:prod', function () {
  var tsProject = ts.createProject('src/tsconfig.json');
  var tsResult = gulp.src(['src/*.ts','src/**/*.ts'])
    .pipe(sourcemaps.init())
    .pipe(tsProject(tsResult))
  return tsResult.js
    .pipe(sourcemaps.write())
    .pipe(uglify())//压缩
    .pipe(gulp.dest('dist'))
});

gulp.task('browser-sync', function () {
   var files = [
      'src/*.html',
      'src/app/*.ts',
      'src/app/**/*.ts',
      'src/app/**/views/*'
   ];

   browserSync.init(files, {
      server: {
         baseDir: './dist'
      }
   });
});

gulp.task('lint', function() {  
  return gulp.src([
      'src/app/*.ts',
      'src/app/**/*.ts'])
    .pipe(eslint())
    .pipe(eslint.format());
});

gulp.task('build', function (callback) {
  runSequence('clean', 'lint', 'views', 'copy:prod', 'ts:prod', callback);
});

gulp.task('dev', function () {
  runSequence('clean', 'lint', 'views', 'copy:dev', 'ts:dev', 'browser-sync', function(){
    gulp.watch([
      'src/app/*.ts',
      'src/app/**/*.ts',
    ], ['ts', 'lint']).on('change', reload);
    gulp.watch([
      'src/*.html',
      'src/app/**/views/*'
    ], ['views', 'lint']).on('change', reload);
  });
});

gulp.task('default', ['dev']);